package com.spirng.leetcode.day06;

public class Test110 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1,null,new TreeNode(3,null,new TreeNode(2)));
        Test110 test110 = new Test110();

    }
    public boolean isBalanced(TreeNode root) {
        int balanced = balanced(root);
        if(balanced==-1){
            return false;
        }else {
            return true;
        }
    }
    public int balanced(TreeNode node){
        if(node==null){
            return 0;
        }
        int left=balanced(node.left);
        if(left==-1){
            return -1;
        }
        int right=balanced(node.right);
        if(right==-1){
            return -1;
        }
        if(Math.abs(left-right)>1){
            return -1;
        }else {
        int result=Math.max(left,right)+1;
        return result;
        }

    }

}
